package si.yun.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;
import si.yun.dao.LoginDao;
import si.yun.entity.SyBeforeUser;
import si.yun.entity.SyLoginRecord;
import si.yun.service.LoginService;
import si.yun.utils.GetRandomUtil;
import si.yun.utils.Md5Utils;
import java.util.List;
import java.util.Map;

/**
 * @program: ThinkManage
 * @description: 移动登录业务层
 * @author: 崔晓晨
 * @create: 2021-10-11 16:26
 **/
@Service
@PropertySource({"config/config.properties"})
public class LoginServiceImpl implements LoginService {
    @Autowired
    private LoginDao loginDao;
    //获取服务器路径
    @Value("${path1}")
    private String path;
    /**
     * @Description  查看用户登录数据
     * @return si.yun.entity.SyBeforeUser 
     * @Date 2021/10/12 20:57
     **/
    @Override
    public SyBeforeUser selBeforeLoginInfo(String phone, String password) {
        SyBeforeUser beforeUser = loginDao.selBeforeLoginInfo(phone);
        return beforeUser;
    }
    /**
     * @Description  注册前台用户
     * @return int
     * @Date 2021/10/13 11:20
     **/
    @Override
    public int registerBeforeUser(String phone, String password,String nickName) {
        SyBeforeUser beforeUser = new SyBeforeUser();
        //加密密码
        String newPassword = Md5Utils.MD5andSAL(password);
        beforeUser.setPassword(newPassword);
        //生成账号
        String account = GetRandomUtil.getRandomCharStr(15);
        beforeUser.setAccount(account);
        //昵称
        beforeUser.setNickname(nickName);
        //手机号码
        beforeUser.setPhone(phone);
        Integer register = loginDao.registerBeforeUser(beforeUser);
        loginDao.addNoteInfo(beforeUser.getUserId(),"Hi，欢迎你来到思云课堂2.0版本");
        return register;
    }
    /**
     * @Description  查看用户是否存在
     * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     * @Date 2021/10/25 14:00
     **/
    @Override
    public List<String> selPhones() {
        List<String> phones = loginDao.selPhones();
        return phones;
    }
    /**
     * @Description  忘记  修改密码
     * @return java.lang.Integer 
     * @Date 2021/10/13 13:52
     **/
    @Override
    public Integer updBeforeUserPwd(String phone, String password) {
        //加密密码
        String newPassword = Md5Utils.MD5andSAL(password);
        Integer upd = loginDao.updBeforeUserPwd(phone, newPassword);
        return upd;
    }

    /**
     * @Description  更新头像
     * @return java.lang.Integer
     * @Date 2021/10/19 19:54
     **/
    @Override
    public Integer updBeforeUserImg(String avatarUrl, Integer userId) {
        Integer upd = loginDao.updBeforeUserImg(avatarUrl, userId);
        return upd;
    }
    /**
     * @Description  修改用户信息
     * @return java.lang.Integer
     * @Date 2021/10/20 10:25
     **/
    @Override
    public Integer updBeforeInfo(SyBeforeUser beforeUser) {
        /*String idCard = beforeUser.getIdCard();
        String year = idCard.substring(6, 10);// 截取年
        String month = idCard.substring(10, 12);// 截取月份
        String day = idCard.substring(12, 14);// 截取天
        String birthDate = year+"-"+month+"-"+day+" "+"00:00:00";
        beforeUser.setBirthDate(birthDate);*/
        Integer upd = loginDao.updBeforeInfo(beforeUser);
        return upd;
    }
    /**
     * @Description  添加登录日志
     * @return java.lang.Integer
     * @Date 2021/11/8 18:53
     **/
    @Override
    public Integer addLoginLogo(SyLoginRecord loginRecord) {
        Integer add = loginDao.addLoginLogo(loginRecord);
        return add;
    }
    /**
     * 第三方登录
     * @param type
     * @param unionId
     * @param nickname
     * @param avatarUrl
     * @return
     */
    @Override
    public SyBeforeUser InicioDeSesiónDeTerceros(String type, String unionId,String nickname, String avatarUrl) {
        SyBeforeUser userInfo = null;
        if ("qq".equals(type)){
            userInfo = loginDao.getBeforeUserByQQId(unionId);
        }else if("weixin".equals(type)){
            userInfo = loginDao.getBeforeUserByWXId(unionId);
        }
        if (userInfo == null){
            //注册用户
            userInfo = registerUserInfo(type,unionId,nickname,avatarUrl);
            if ("qq".equals(type)){
                userInfo = loginDao.getBeforeUserByQQId(unionId);
            }else if("weixin".equals(type)){
                userInfo = loginDao.getBeforeUserByWXId(unionId);
            }
        }
        return userInfo;
    }

    /**
     * 去除第三方登录重复操作
     * @param type
     * @param unionId
     * @param nickname
     * @param avatarUrl
     * @return
     */
    public SyBeforeUser registerUserInfo(String type, String unionId, String nickname, String avatarUrl) {
        SyBeforeUser userInfo = new SyBeforeUser();
        if ("qq".equals(type)){
            //设置qq唯一标识
            userInfo.setQqUnionId(unionId);
        }else if("weixin".equals(type)){
            //设置微信唯一标识
            userInfo.setWxUnionId(unionId);
        }
        //设置昵称
        userInfo.setNickname(nickname);
        //设置头像
        userInfo.setAvatarUrl(avatarUrl);
        //生成账号
        userInfo.setAccount(GetRandomUtil.getRandomCharStr(15));
        //账号状态
        userInfo.setStatus(2);
        //账号初始签名
        userInfo.setFreestyle("Hi，欢迎来到思云课堂");
        //注册账户
        Integer i = loginDao.addBeforeUserByWxOrQq(userInfo);
        //发送通知
        loginDao.addNoteInfo(userInfo.getUserId(),"Hi，欢迎你来到思云课堂2.0版本");
        return userInfo;
    }
    /**
     * @Description  获取用户信息
     * @return si.yun.entity.SyBeforeUser
     * @Date 2021/12/14 15:00
     **/
    @Override
    public SyBeforeUser selUserAllInfo(Integer userId) {
        SyBeforeUser beforeUser = loginDao.selUserAllInfo(userId);
        if(beforeUser.getAvatarUrl()!=null && beforeUser.getAvatarUrl().length()!=0 && beforeUser.getAvatarUrl()!="null"){
            if(beforeUser.getAvatarUrl().indexOf("http") == -1){
                beforeUser.setAvatarUrl(path+beforeUser.getAvatarUrl());
            }
        }
        return beforeUser;
    }
    /**
     * @Description  短信快速登录
     * @return si.yun.entity.SyBeforeUser
     * @Date 2021/12/24 11:45
     **/
    @Override
    public SyBeforeUser selPhoneLoginInfo(String phone) {
        SyBeforeUser beforeUser = loginDao.selBeforeLoginInfo(phone);
        if(beforeUser.getAvatarUrl()!=null && beforeUser.getAvatarUrl().length()!=0 && beforeUser.getAvatarUrl()!="null"){
            if(beforeUser.getAvatarUrl().indexOf("http") == -1){
                beforeUser.setAvatarUrl(path+beforeUser.getAvatarUrl());
            }
        }
        return beforeUser;
    }
    /**
     * @Description  短信快速注册
     * @return int
     * @Date 2021/12/24 11:58
     **/
    @Override
    public int phoneRegisterUser(String phone, String nickName) {
        SyBeforeUser beforeUser = new SyBeforeUser();
        //生成账号
        String account = GetRandomUtil.getRandomCharStr(15);
        beforeUser.setAccount(account);
        //昵称
        beforeUser.setNickname(nickName);
        //手机号码
        beforeUser.setPhone(phone);
        Integer register = loginDao.phoneRegisterUser(beforeUser);
        if(register!=0){
            loginDao.addNoteInfo(beforeUser.getUserId(),"Hi，欢迎你来到思云课堂2.0版本");
        }
        return register;
    }
    /**
     * @Description  获取用户的头像path 删除服务器中旧的头像
     * @return java.util.Map<java.lang.String,java.lang.String>
     * @Date 2021/12/26 19:19
     **/
    @Override
    public Map<String, String> selOldUserImg(Integer userId) {
        Map<String, String> stringStringMap = loginDao.selOldUserImg(userId);
        return stringStringMap;
    }
}
